import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import Permission from '@/store/entities/administration/permission/permission';
import Ajax from '@/lib/ajax';
import PageResult from '@/store/entities/page-result';


interface PermissionState extends ListState<Permission> {
    permissions: Permission[];
    filterPermissions: Permission[];
}


class PermissionModule extends ListModule<PermissionState, any, Permission> {
    state = {
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: [],
        loading: false,
        permissions: [],
        filterPermissions: [],
    };
    actions = {
        async getAllPermissions(context: ActionContext<PermissionState, any>) {
            const response = await Ajax.get('/api/services/app/Permission/GetAllPermissions')
            context.state.permissions = response.data.result.items;
        },
    };
    mutations = {
        filterPermissions(state: PermissionState, payload: string) {
            state.filterPermissions = state.permissions.filter(item => {
                return !item.leaf || (item.leaf && item.displayName.toLowerCase().indexOf(payload.toLowerCase()) >= 0);
            });
        },
    };
}

const permissionModule = new PermissionModule();
export default permissionModule;
